package com.example.demo.web;

import com.example.demo.core.Result;
import com.example.demo.core.ResultGenerator;
import com.example.demo.model.Student;
import com.example.demo.service.StudentService;
import com.example.demo.service.impl.StudentServiceImpl;
import com.example.demo.support.MyFileUtil;
import com.example.demo.support.PoiUtils;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import lombok.extern.slf4j.Slf4j;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.util.ResourceUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @author Kevin
 * @date 2020/4/19 16:51
 */
@RestController
@RequestMapping("/student")
@Slf4j
public class StudentController {
    @Resource
    private StudentService studentService;

    @PostMapping("/add")
    public Result add(Student user) {
        studentService.save(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/delete")
    public Result delete(@RequestParam Integer id) {
        studentService.deleteById(id);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/update")
    public Result update(Student user) {
        studentService.update(user);
        return ResultGenerator.genSuccessResult();
    }

    @PostMapping("/detail")
    public Result detail(@RequestParam Integer id) {
        Student user = studentService.findById(id);
        return ResultGenerator.genSuccessResult(user);
    }

    @PostMapping("/list")
    public Result list(@RequestParam(defaultValue = "0") Integer page, @RequestParam(defaultValue = "0") Integer size) {
        PageHelper.startPage(page, size);
        List<Student> list = studentService.findAll();
        PageInfo pageInfo = new PageInfo(list);
        return ResultGenerator.genSuccessResult(pageInfo);
    }

    @RequestMapping("/download")
    public void DownloadExecl(HttpServletRequest request, HttpServletResponse response, HttpSession session) throws IOException {
        // 先服务器本地备份
        List<Student> list = studentService.findAll();
        HSSFWorkbook workBook = PoiUtils.getWorkBook(list);
        FileOutputStream fileOutputStream = null;
        String downloadPath = ResourceUtils.getFile("classpath:").getAbsolutePath();
        String fileName = "download.xls";
        try {
            fileOutputStream = new FileOutputStream(new File(downloadPath,"static/upload_file/"+fileName));
            workBook.write(fileOutputStream);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        // 然后重定向下载，后期可以优化
        // response.setHeader("content-disposition","attachment;fileName"+ URLEncoder.encode("/static/upload_file/"+fileName,"UTF-8"));
        // response.sendRedirect("/file/download?download.xls");
        // 重定向失败，直接下载吧
        MyFileUtil.downloadHelper("download.xls", response);
    }
}
